//
//  LDArtifactGiftController.m
//  Gift
//
//  Created by 李迪 on 15/11/18.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDArtifactGiftController.h"
#import "LDCollectionViewCell.h"
#import "BaseHeader.h"
#import "LDArtifactModel.h"

@interface LDArtifactGiftController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, assign)NSInteger page;

@end

@implementation LDArtifactGiftController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.collection release];
    [super dealloc];
}

#pragma mark - 创建瀑布流
- (void)createrCollection{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width/2-20, 250);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 3;
    flowLayout.minimumLineSpacing = 1;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 15);
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;

    [self.collection registerClass:[LDCollectionViewCell class] forCellWithReuseIdentifier:@"collectionHot"];
    [self.view addSubview:self.collection];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        self.collection.backgroundColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNot:) name:@"lidi" object:nil];
    [self.collection release];
}

#pragma mark - 夜间模式
- (void)handleNot:(NSNotification *)Not{
    NSString *passWord = [Not object];
    if ([passWord isEqualToString:@"yes"]) {
        self.collection.backgroundColor = [UIColor blackColor];
    }else if ([passWord isEqualToString:@"no"]){
        self.collection.backgroundColor = [UIColor whiteColor];
    }
}

#pragma mark - 瀑布流协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.array.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionHot" forIndexPath:indexPath];
    SinglePieceModel *model = [self.array objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - 数据请求
- (void)handleDataNum:(NSInteger)number{
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=40&offset=%ld",self.num,number];

    [PFAFNetworking getUrl:str body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        if (number == 0) {
            self.array = [[NSMutableArray array]init];
        }
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"items"]) {
            SinglePieceModel *model = [SinglePieceModel modelSetDic:dic];
            [self.array addObject:model];
        }
        [self.collection reloadData];
        [self.collection.header endRefreshing];
        [self.collection.footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self.collection.header endRefreshing];
        [self.collection.footer endRefreshing];
    }];
    
}

-(void)addHeader
{
    self.collection.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 0;
        [self handleDataNum:self.page];
    }];
    [self.collection.header beginRefreshing];
}

- (void)addFoot{
    
    self.collection.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page + 20;
        [self handleDataNum:self.page];
    }];
    [self.collection.footer beginRefreshing];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LDSingieceWeb *web = [[LDSingieceWeb alloc]init];
    LDArtifactModel *model = [self.array objectAtIndex:indexPath.row];
    web.strWeb = model.url;
    [self.navigationController pushViewController:web animated:YES];
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createrCollection];
    self.page = 0;
    [self addHeader];
    [self addFoot];
    self.title = @"神器详情";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
